/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crystalrange.documents.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import org.crystalrange.documents.model.DocumentTag;
import org.crystalrange.documents.service.DocumentTagService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author Krzysiek
 */
public class DocumentTagSelectController extends GenericForwardComposer {
   
    protected DocumentTagService documentTagService;
    protected DocumentTag selectedDocumentTag;
    protected List<DocumentTag> documentTags;
    protected Search searchDocumentTag;
    protected AnnotateDataBinder binder;

    public DocumentTagService getDocumentTagService() {
        return documentTagService;
    }

    public void setDocumentTagService(DocumentTagService documentTagService) {
        this.documentTagService = documentTagService;
    }

    public List<DocumentTag> getDocumentTags() {
        return documentTags;
    }

    public void setDocumentTags(List<DocumentTag> documentTags) {
        this.documentTags = documentTags;
    }

    public DocumentTag getSelectedDocumentTag() {
        return selectedDocumentTag;
    }

    public void setSelectedDocumentTag(DocumentTag selectedDocumentTag) {
        this.selectedDocumentTag = selectedDocumentTag;
    }
    
    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        this.searchDocumentTag = new Search(DocumentTag.class);
        this.searchDocumentTag.setMaxResults(100);
        this.searchDocumentTag.addFetch("createdBy");
        this.documentTags = this.documentTagService.search(this.searchDocumentTag);
        component.setAttribute("controller", this);
    }

    public void onClick$buttonOk(Event event) throws Exception {
        if (this.selectedDocumentTag != null) {
            this.execution.setAttribute("selectedDocumentTag", this.selectedDocumentTag);
            this.execution.setAttribute("result", "ok");
            this.self.detach();
        } else {
            Messagebox.show("Please select a Document Tag.");
        }
    }

    public void onClick$buttonCancel(Event event) throws Exception {
        this.execution.setAttribute("selectedDocumentTag", null);
        this.execution.setAttribute("result", "cancel");
        this.self.detach();
    }
    
    public void onClick$buttonSearch(Event event) throws Exception {
        this.execution.setAttribute("searchDocumentTag", this.searchDocumentTag);

        Window window = (Window) this.execution.createComponents("~./zul/html/documentTagSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchDocumentTag = (Search) this.execution.getAttribute("searchDocumentTag");
        }

        this.documentTags = this.documentTagService.search(this.searchDocumentTag);
        this.binder.loadAll();
    }
}
